package br.lsi.supercurriculum.daos;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;

import br.lsi.supercurriculum.entidades.Endereco;
import br.lsi.supercurriculum.interfaces.EnderecoFacade;
import br.lsi.supercurriculum.util.HibernateUtil;


/**
 * @author Alexsandro Guedes
 * @since 23/08/2012
 */

/**
 *  Classe de persistencia da entidade Endereco
 */

public class EnderecoDaoImpl implements EnderecoFacade{
	
	@Override
	public void salvar(Endereco endereco) {
		Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        session.save(endereco);
        t.commit();
	}

	@Override
	public Endereco getEndereco(String cep) {
		Session session = HibernateUtil.getSessionFactory().openSession();
        return (Endereco) session.load(Endereco.class, cep);
	}

	@Override
	public List<Endereco> listarEnderecos() {
		 Session session = HibernateUtil.getSessionFactory().openSession();
	     Criteria c = session.createCriteria(Endereco.class);
	     return c.list();
	}

	@Override
	public void excluir(Endereco endereco) {
		Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        session.delete(endereco);
        t.commit();
		
	}

	@Override
	public void atualizar(Endereco endereco) {
		Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction t = session.beginTransaction();
        session.update(endereco);
        t.commit();
		
	}

}